package com.example.android.helloactivity;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.DisplayMetrics;
import android.view.View;

public class ChessView extends View {
    

	public static final int QIPANSIZE = 15; 
	
	//
	//the chessboarder
	//
	int screenWidth;
	int screenHeight;
	
	//
	//calc the value
	//
    int side;
	int pitch;
	int startPoint;
	int radius;
	
	int x;
	int y;
	Paint paint;
	//int background;
	int linecolor;
	
	Situation first;//person
	Situation second;//computer
	
	Boolean needInitGraphy;
	int numPoint;//how many points to choice
	

    public ChessView(Context context,int playWithPhone,int selectColor,String player_black,String player_white) {
    	
        super(context);
        paint = new Paint();
        //background = Color.GREEN;
        linecolor = Color.BLACK;
        x = -1;
        y = -1;
        needInitGraphy = true;
        //this value is very important
        numPoint = 1;

        if(1==playWithPhone){
	        
        	if(0 == selectColor){
	        	first = new Situation(Color.BLACK,true,player_black);
	        	second = new Situation(Color.WHITE,false,(String)"phone");
	        }else{
	        	first = new Situation(Color.WHITE,false,player_white);
	        	second = new Situation(Color.BLACK,true,(String)"phone");
	        	second.situation[QIPANSIZE/2][QIPANSIZE/2]=1;
	        	second.history[0].px=QIPANSIZE/2;
	        	second.history[0].py=QIPANSIZE/2;
	        	second.history[0].invalid=1;
	        	second.index=1;
	        	postInvalidate();
	        	first.turn=true;
	        	second.turn=false;
	        }
        }else{
        	first = new Situation(Color.BLACK,true,player_black);
        	second = new Situation(Color.WHITE,false,player_white);
        }
        

        //
        //get the highth and width of screen
        //
        
        DisplayMetrics dm = new DisplayMetrics(); 
    	
        dm = context.getApplicationContext().getResources().getDisplayMetrics(); 
        screenWidth = dm.widthPixels; 
        screenHeight = dm.heightPixels; 
        //Log.d("DEBUG","width="+screenWidth+"   height="+screenHeight);
    	//
    	//get the shorter side as the boarder
    	//
        side = screenWidth>screenHeight?screenHeight:screenWidth;
    	pitch = side/QIPANSIZE;
    	startPoint = (pitch + side%QIPANSIZE)/2;
    	radius = (int) (pitch*0.4);
    }
    
    public void InitialChessboard(Canvas canvas){
    	
    	//
    	//the background of canvas
    	//
    	//canvas.drawColor(background); 
    	
    	//
    	//draw the qipan
    	//
    	
    	paint.setColor(linecolor);
    	for(int i=0;i<QIPANSIZE;i++){
    		
    		//
    		//draw row
    		//
    		canvas.drawLine(startPoint, i*pitch+startPoint, (QIPANSIZE-1)*pitch+startPoint, i*pitch+startPoint, paint);
    		
    		//
    		//draw colum
    		//
    		canvas.drawLine(i*pitch+startPoint, startPoint, i*pitch+startPoint, (QIPANSIZE-1)*pitch+startPoint, paint);
    	}
    }
    
    public boolean isUpdate(){
    	

    	if(x >= startPoint && x <= side && y >= startPoint && y <= side){
    		
        	int px = 0;
    		int py = 0;
    		int locate = 0;
    		int remainder = 0;
    		
    		locate = (x - startPoint)/pitch;
    		remainder = (x - startPoint)%pitch;
    		px = locate+(remainder>(pitch/2)?1:0);
    		locate = (y - startPoint)/pitch;
    		remainder = (y - startPoint)%pitch;
    		py = locate+(remainder>(pitch/2)?1:0);
    		
    		//
    		//check if update and change the turn at the same time
    		//
    		if(px >= 0 && px <QIPANSIZE && py >= 0 && py < QIPANSIZE && 1 != first.situation[px][py] && 1 != second.situation[px][py]){
    		
	    		if(true == first.turn){
	    			
	    			first.situation[px][py] = 1;
	    			first.history[first.index].px=px;
	    			first.history[first.index].py=py;
	    			first.history[first.index].invalid=1;
	    			first.index += 1;
	    		}else {
	    			second.situation[px][py] = 1;
	    			second.history[second.index].px=px;
	    			second.history[second.index].py=py;
	    			second.history[second.index].invalid=1;
	    			second.index += 1;
	    		}
	    		return true;
    		}
    	}
    	
    	return false;
    }

    
    protected void onDraw(Canvas canvas) {
    	
    	//
    	//draw the boarder
    	//
    	
    	InitialChessboard(canvas);
    	
    	//
    	//draw the situation
    	//
    	int px = 0;
    	int py = 0;
		
		//using the index,this method is good
    	paint.setStyle(Style.FILL);
		paint.setColor(first.color);
		for(int i=0;i<=first.index;i++){
			if(1 != first.history[i].invalid){

				continue;
			}
			//paint.setColor(first.color);
    		px = first.history[i].px*pitch + startPoint;
    		py = first.history[i].py*pitch + startPoint;
			canvas.drawCircle(px, py, radius, paint);
		}
		
		//mark the last qizi
		canvas.drawCircle(px, py, radius, paint);
		paint.setStyle(Style.FILL);
		paint.setColor(Color.GREEN);
		canvas.drawCircle(px, py, radius/3, paint);
		
		paint.setStyle(Style.FILL);
		paint.setColor(second.color);
		for(int i=0;i<=second.index;i++){
			if(1 != second.history[i].invalid){

				continue;
			}
			//paint.setColor(second.color);
    		px = second.history[i].px*pitch + startPoint;
    		py = second.history[i].py*pitch + startPoint;
			canvas.drawCircle(px, py, radius, paint);
		}
		
		//mark the last qizi
		canvas.drawCircle(px, py, radius, paint);
		paint.setStyle(Style.FILL);
		paint.setColor(Color.GREEN);
		canvas.drawCircle(px, py, radius/3, paint);
		
		//reset the switch value
		x = -1;
		y = -1;
    }
}// end of chessview

